package com.zzl.study.manager;

import com.zzl.study.entity.User;
import org.springframework.stereotype.Component;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @author: zhangzl
 * @date: 2024/3/6 16:08
 * @version: 1.0
 * @description: 用户在线管理器
 */
@Component
public class UserOnlineManager {

    private final ConcurrentHashMap<Long, User> userContainer = new ConcurrentHashMap<>();

    public void online(User user) {
        this.userContainer.put(user.getId(), user);
    }

    public void offline(Long id) {
        this.userContainer.remove(id);
    }

    public double getOnlineUserCount() {
        return Double.parseDouble(String.valueOf(this.userContainer.size()));
    }

}
